<template>
    <div style="padding: 60px;font-size: 20px;font-weight: 600;">

        正在登录中,请稍等...
    </div>
</template>

<script>
import request from "@/utils/request";
import { setToken } from "@/utils/auth";
import store from "@/store";
import { log } from "vxe-pc-ui";


export default {
    name: "cas",
    data() {
        return {
            token: "",
            headerT: "",
            redirect: ""
        };
    },
    watch: {
        // 监听路由变化
        $route: {
            handler: function (route) {
                // 重定向
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true,
        },
    },
    async mounted() {
        this.token = this.$route.query.token;
        this.headerT = this.$route.query.t;
        if (this.token) {
            try {
                const resData = await request({
                    url: `/casLogin`,
                    method: 'get',
                    params: { token: this.token, t: this.headerT }
                }).then(res => res.data)

                store.commit('user/SET_TOKEN', resData.token)
                store.commit('user/SET_USER_INFO', resData)
                setToken(resData.token)
                this.$router.push({ path: '/' })


            } catch (err) {
                throw new Error(err)
            }
        } else if (this.headerT && this.headerS) {
            this.loginFast()
        }
    },
    methods: {
        loginFast() {
            let fastObj = {
                t: this.headerT,
                s: this.headerS,
            };
            if (this.headerS && this.headerT) {
                loginFast(fastObj).then((res) => {
                    if (res.status) {
                        setStore("accessToken", res.data.token);
                        setStore("userInfo", res.data);
                        this.$router.push("/home");
                    } else {
                        this.$router.push("/pages/error-500");
                    }
                });
            }
        },
    },
};
</script>